﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using UnityEngine;

    internal class ObjectBrowserTreeViewDragging : DefaultTreeViewDragging
    {
        public ObjectBrowserTreeViewDragging(TreeView treeView) : base(treeView, HierarchyType.Assets)
        {
        }

        public override DragAndDropVisualMode DoDrag(TreeView.Node parentNode, TreeView.Node targetNode, bool perform)
        {
            if (targetNode == null)
            {
                return DragAndDropVisualMode.None;
            }
            object genericData = DragAndDrop.GetGenericData(ProjectWindowUtil.k_DraggingFavoriteGenericData);
            if (genericData != null)
            {
                int instanceID = (int) genericData;
                if (!(targetNode is SearchFilterTreeNode) || !(parentNode is SearchFilterTreeNode))
                {
                    return DragAndDropVisualMode.None;
                }
                bool flag = SavedSearchFilters.CanMoveSavedFilter(instanceID, parentNode.instanceID, targetNode.instanceID, true);
                if (flag && perform)
                {
                    SavedSearchFilters.MoveSavedFilter(instanceID, parentNode.instanceID, targetNode.instanceID, true);
                }
                return (!flag ? DragAndDropVisualMode.None : DragAndDropVisualMode.Copy);
            }
            if (!(targetNode is SearchFilterTreeNode) || !(parentNode is SearchFilterTreeNode))
            {
                return base.DoDrag(parentNode, targetNode, perform);
            }
            string str = DragAndDrop.GetGenericData(ProjectWindowUtil.k_IsFolderGenericData) as string;
            if (!(str == "isFolder"))
            {
                return DragAndDropVisualMode.None;
            }
            if (perform)
            {
                UnityEngine.Object[] objectReferences = DragAndDrop.objectReferences;
                if (objectReferences.Length > 0)
                {
                    HierarchyProperty property = new HierarchyProperty(HierarchyType.Assets);
                    if (property.Find(objectReferences[0].GetInstanceID(), null))
                    {
                        SearchFilter filter = new SearchFilter();
                        string assetPath = AssetDatabase.GetAssetPath(property.instanceID);
                        if (!string.IsNullOrEmpty(assetPath))
                        {
                            filter.folders = new string[] { assetPath };
                            bool addAsChild = targetNode == parentNode;
                            float listAreaGridSize = ObjectBrowserTreeViewGUI.GetListAreaGridSize();
                            Selection.activeInstanceID = SavedSearchFilters.AddSavedFilterAfterInstanceID(property.name, filter, listAreaGridSize, targetNode.instanceID, addAsChild);
                        }
                        else
                        {
                            Debug.Log("Could not get asset path from instanceID " + property.name);
                        }
                    }
                }
            }
            return DragAndDropVisualMode.Copy;
        }

        public override void StartDrag(TreeView.Node draggedNode, List<int> selectedNodes)
        {
            if (!SavedSearchFilters.IsSavedFilter(draggedNode.instanceID) || (draggedNode.instanceID != SavedSearchFilters.GetRootInstanceID()))
            {
                ProjectWindowUtil.StartDrag(draggedNode.instanceID, selectedNodes);
            }
        }
    }
}

